package com.yunst.view;

import com.yunst.net.NetClient;
import com.yunst.net.handle.ChannelInitializerHandle;
import com.yunst.net.message.EventEnum;
import com.yunst.net.message.EventMessage;
import com.yunst.utils.ImgUtils;
import io.netty.util.internal.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @author yunst
 * @date 2022/12/27
 */
public class Window extends JFrame {
    private static JLabel jLabel;
    static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    private static int width;
    private static int height;

    public static void setImage(byte[] bytes) {
        ImageIcon imageIcon = new ImageIcon(bytes);
        height = imageIcon.getIconHeight();
        width = imageIcon.getIconWidth();


        BufferedImage image = ImgUtils.scaleImage((int) screenSize.getWidth(), (int) screenSize.getHeight() - 30, imageIcon);
        jLabel.setIcon(new ImageIcon(image));

    }

    public static void errorMessage() {
        URL resource = Window.class.getClassLoader().getResource("images/error.png");

        ImageIcon icon = new ImageIcon(resource);
        JOptionPane.showMessageDialog(null, "连接失败，请检查设备ip地址✅", "提示", JOptionPane.WARNING_MESSAGE, icon);
    }

    public Window() {
        System.out.println(screenSize.height);
        URL resource = Window.class.getClassLoader().getResource("images/add.png");

        jLabel = new JLabel(new ImageIcon(resource));

        this.add(jLabel);
        this.setSize((int) screenSize.getWidth() / 2, (int) screenSize.getHeight() / 2);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (ChannelInitializerHandle.channelHandlerContext != null) {
                    int x = (int) (e.getX() * width / screenSize.getWidth());
                    int y = (int) (e.getY() * height / (screenSize.getHeight() - 30));
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(EventEnum.CLICK.getType());
                    stringBuilder.append(":");
                    stringBuilder.append(x);
                    stringBuilder.append(":");
                    stringBuilder.append(y);
                    EventMessage eventMessage = new EventMessage();
                    eventMessage.setLen(stringBuilder.length());
                    eventMessage.setContent(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
                    ChannelInitializerHandle.channelHandlerContext.writeAndFlush(eventMessage);

                } else {
                    URL resource = Window.class.getClassLoader().getResource("images/control.png");
                    ImageIcon imageIcon = new ImageIcon(resource);
                    String ip = (String) JOptionPane.showInputDialog(null, "请输入要连接设备ip地址", "输入", JOptionPane.INFORMATION_MESSAGE, imageIcon, null, null);
                    if (StringUtil.isNullOrEmpty(ip)) {
                        return;
                    }
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            new NetClient(ip);
                        }
                    }).start();
                }

            }
        });

    }


}
